package com.zfjt.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.zfjt.constants.InterfaceDefinition;
import com.zfjt.entity.BrabchBean;
import com.zfjt.entity.ClientmessageBean;
import com.zfjt.entity.ReturnBean;
import com.zfjt.entity.UserBean;
import com.zfjt.packnum.PackNum00038;
import com.zfjt.service.BrabchService;
import com.zfjt.service.ClientmessageBeanService;
import com.zfjt.service.UserService;
import com.zfjt.utils.StringUtil;

/**
 * 输入通过金额或驳回理由
 * @author xk
 * @version 1.0
 * @date 2017年4月25日
 */
@Controller
public class MoneyOrderController {
	
	private ReturnBean mReturn;
	
	@Autowired
	private ClientmessageBeanService mClientMessage;
	
	@Autowired
	private UserService mUser;
	
	@Autowired
	private BrabchService mBrabch;
	
	private Map<String, String> fhdata; 
	
	@RequestMapping(value="00038")
	@ResponseBody
	public ReturnBean AddBrabch(HttpSession hseesion,HttpServletRequest request){
		mReturn = new ReturnBean();
		fhdata = new HashMap<>();
		//传过来的参数
		String data =  (String) hseesion.getAttribute("data");
		PackNum00038 num00038 = JSON.parseObject(data,PackNum00038.class); 	
		List<ClientmessageBean> mData = mClientMessage.selectAll();
		if (data.equals(InterfaceDefinition.IStatusCode.TOKEN_FAILURE)) {//校验码错误
			mReturn.setCode("001");
			mReturn.setPortnum("00037");
			fhdata.put("error", "TOKEN_ERROR");
			mReturn.setData(JSON.toJSONString(fhdata));
		}else{
			ClientmessageBean dg = mClientMessage.selectByPrimaryKey((short)Integer.parseInt(num00038.getId()));
			dg.setMoney(num00038.getMoney());	
			if (num00038.getType().equals("1")) {
				if (StringUtil.isNotEmpty(dg.getPasstime())) {//Passtime不为空代表已经输入过通过金额，不可以再输入
					mReturn.setCode("002");
					mReturn.setPortnum("00038");
					fhdata.put("error", "ALREADY");
					mReturn.setData(JSON.toJSONString(fhdata));
				}else{//Passtime为空代表没输入过通过金额，可以输入
					//设置通过时间
					dg.setPasstime(num00038.getTime());
					//如果类型是输入金额则把此金额算入个人业绩
					UserBean user = mUser.selectByPrimaryKey(dg.getAffiliationYewu());
					int money1 = Integer.parseInt(user.getText());
					money1 = money1 + Integer.parseInt(num00038.getMoney());
					user.setText(money1 + "");
					mUser.updateByPrimaryKeySelective(user);
					//如果类型是输入金额则把此金额算入网点业绩
					BrabchBean brabch = mBrabch.selectByPrimaryKey(user.getBranch());
					int money2 = Integer.parseInt(brabch.getAffiliation());
					money2 = money2 + Integer.parseInt(num00038.getMoney());
					brabch.setAffiliation(money2 + "");
					mBrabch.updateByPrimaryKeySelective(brabch);
					mClientMessage.updateByPrimaryKeySelective(dg);
					mReturn.setCode("000");
					mReturn.setPortnum("00038");
					fhdata.put("chenggong", "chenggong");
					mReturn.setData(JSON.toJSONString(fhdata));
				}				
			}else{
				mClientMessage.updateByPrimaryKeySelective(dg);
				mReturn.setCode("000");
				mReturn.setPortnum("00038");
				fhdata.put("chenggong", "chenggong");
				mReturn.setData(JSON.toJSONString(fhdata));
			}
		}
		return mReturn;
	}
}
